<?php
require_once(dirname(dirname(__FILE__)) . '/app.php');

$cur_foot = 'mygroups';
check_login();
$pagetitle = "我的团";

$condition = array( 'user_id' => $login_user_id, 'team_id > 0', 'is_pin'=>1);

$count = Table::Count('order', $condition);

list($pagesize, $offset, $pagestring) = pagestring($count, 10);

$orders = DB::LimitQuery('order', array(
	'condition' => $condition,
	'order' => 'ORDER BY create_time DESC, id ASC',
	'size' => $pagesize,
	'offset' => $offset,
));

foreach($orders as &$order_info)
{
	$pin_order_list = DB::LimitQuery('pin_order_list',array(
			'condition' => array('order_id'=>$order_info['id']),
			'one'=>true
		));
	
	$tmp_pin_order = Table::Fetch('pin_order', $pin_order_list['pin_id']);
	
	$refund_tip = '';
	
	if($order_info['state']=='unpay' && $order_info['rstate']=='berefund')
	{
		$refund_tip = '已退款';
	}else if($order_info['rstate']=='askrefund')
	{
		$refund_tip = '退款中';
	}else if($order_info['state']=='unpay'){
		$refund_tip = '未支付';
	}else if($order_info['state']=='pay'){
		$refund_tip = '已支付';
	}
		
	if($tmp_pin_order['state'] == 1)
	{
		$order_info['pin_tip'] = '拼团已成功.'.$refund_tip;
	}
	else if($tmp_pin_order['state'] == 2 || $tmp_pin_order['endtime'] <= time())
	{
		$order_info['pin_tip'] = '拼团已失败.'.$refund_tip;
	}else {
		$order_info['pin_tip'] = '拼团中.'.$refund_tip;
	}
}

$team_ids = Utility::GetColumn($orders, 'team_id');
$teams = Table::Fetch('team', $team_ids);
foreach($teams AS $tid=>$one){
	team_state($one);
	$teams[$tid] = $one;
}

if(isset($_GET['page'])  && intval($_GET['page']) > 1)
{
	$s_html = '';
	$result = array('ret'=>1,'offset'=>$offset);
	if(!empty($orders)){
		$result['ret'] = 0;
	}
	foreach($orders as $index =>$one)
	{
		
		$s_html .= '	<div class="mt_g">';
		$s_html .= '		<div class="mt_g_img">';
		$s_html .= '			<a href="/account/group.php?id='.$one['id'].'">';
		$s_html .= '				<img src="'.team_image($teams[$one['team_id']]['image']).'">';
		$s_html .= '			</a>';
		$s_html .= '		</div>';
		$s_html .= '		<div class="mt_g_info">';
		$s_html .= '			<p class="mt_g_name">'.$teams[$one['team_id']]['title'].'</p>';
		$s_html .= '			<p class="mt_g_price">成团价： <span> <b > ¥'.moneyit($one['origin']).'</b>/件 </span><i></i > </p>';
		$s_html .= '		</div>';
		$s_html .= '	</div> ';
		$s_html .= '	<div class = "mt_status" >';
		$s_html .= '		<span class = "mt_status_txt" > '.$one['pin_tip'].' </span>';
		if(isset($INI['system']['secsite']) && !empty($INI['system']['secsite']))
			$s_html .= '		<a class = "mt_status_lk" href = "'.$INI['system']['wwwprefix'].$INI['system']['secsite'].'/account/group.php?id='.$one['id'].'" > 查看团详情 </a> ';
		else 
			$s_html .= '		<a class = "mt_status_lk" href = "'.$INI['system']['wwwprefix'].'/account/group.php?id='.$one['id'].'" > 查看团详情 </a> ';
	
		$s_html .= '	</div>';
		
	}
	
	$result['html'] = $s_html;
	echo json_encode($result);
	die();
}


include template('order_groups');
